package light.head.dao.inv;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.inv.InvAllocate;

@Service
public class InvAllocateDAO extends BaseDAO<InvAllocate, Integer>{
	public RESULT findByMap(List<Integer> ocs, Map<String, String> map, PageBean pb) {
		if(ocs == null || ocs.size() == 0) return null;
			
		StringBuffer hql = new StringBuffer("FROM InvAllocate ia WHERE (ia.oc IN (");
		List<Object> list = new ArrayList<Object>();
		
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		hql.append(" or ia.ioc IN (");
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append("))");
		if (map != null) {
			String dn =map.get(Parm.DN);
			String bn = map.get(Parm.BN);
			String cn = map.get(Parm.CN);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String deleted = map.get(Parm.DELETED);
			
			
			if (isNotNull(deleted)) {
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND ia.deleted = ?");
			}
			if (isNotNull(dn)) {
				hql.append(" AND ia.dn LIKE ?");
				list.add("%" + dn + "%");
			}
			if (isNotNull(bn)) {
				hql.append(" AND ia.bname LIKE ?");
				list.add("%" + bn + "%");
			}
			if (isNotNull(cn)) {
				hql.append(" AND ia.cn IN (SELECT ui.code FROM UserInfo ui WHERE ui.realName LIKE ?)");
				list.add("%" + cn + "%");
			}if (isNotNull(time1)) {
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append(" AND ia.ct >= ?");
			}
			if (isNotNull(time2)) {
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append(" AND ia.ct <= ?");
			}
		}
		hql.append(" ORDER BY ia.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}

}
